package com.example.supernote.ui.components

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.rememberScrollState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.outlined.KeyboardArrowUp
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

/**
 * 统一的下拉框样式
 *
 * @param buttonModifier 下拉框按钮的修饰符
 * @param itemsModifier 下拉框选项的修饰符
 * @param menuItems 子选项字典
 * @param menuButtonEnabled 下拉框按钮是否可用
 * @param menuButtonText 下拉框按钮的文本
 * @param onSelect 子选项的回调函数
 */
@Composable
fun UniformDropMenu(
    buttonModifier: Modifier = Modifier,
    itemsModifier: Modifier = Modifier.heightIn(max = 300.dp),
    menuItems: Map<String, String>,
    menuButtonEnabled: Boolean,
    menuButtonText: String,
    onSelect: (key: String, value: String) -> Unit
) {
    var isExposedDropdownOpen by remember { mutableStateOf(false) }
    Column {
        DropdownMenu(
            expanded = isExposedDropdownOpen,
            onDismissRequest = { isExposedDropdownOpen = false },
            modifier = itemsModifier,
            scrollState = rememberScrollState(),
        ) {
            for ((key, value) in menuItems) {
                DropdownMenuItem(
                    text = { Text(key) },
                    onClick = {
                        onSelect(key, value)
                        isExposedDropdownOpen = false
                    }
                )
            }
        }
        UniformButton(
            onClick = { isExposedDropdownOpen = !isExposedDropdownOpen },
            modifier = buttonModifier,
            enabled = menuButtonEnabled,
            text = menuButtonText,
            imageVector = if (isExposedDropdownOpen) Icons.Outlined.KeyboardArrowUp else Icons.Filled.KeyboardArrowDown
        )
    }
}